﻿/******************************************************************************
 * 【本类功能概述】                                 					      *
 *  版权所有（C）2017-                                           *
 *  保留所有权利。                                                            *
 ******************************************************************************
 *  作者 : <hughjk>
 *  创建时间:2020年11月14日 15:33:56
 *  文件描述:压缩文件 文件夹
 *****************************************************************************/

using Compress;
using Compress.LZMA;
using Compress.UPK;
using System.IO;
using UnityEngine;
using Util;

public class CompressTool
{
    // private void Awake()
    // {
    //
    //     FileIO.CreateLoadPath(PathConfig.GameData + "/pictur/", "Tea.txt");
    // }
    // [Button]
    // public void OpFind()
    // {
    //     FileIO.OpenDirectory(PathConfig.GameData);
    // }
    // void OnGUI()
    // {
    //     if (GUI.Button(new Rect(Screen.width / 4 * 1, Screen.height / 4 * 1, 140, 80), "UPK打包文件夹"))
    //     {
    //         PackFolderAsync();
    //     }
    //     if (GUI.Button(new Rect(Screen.width / 4 * 1, Screen.height / 4 * 2, 140, 80), "UPK解包文件夹"))
    //     {
    //         UnPackFolderAsync();
    //     }
    //     if (GUI.Button(new Rect(Screen.width / 4 * 2, Screen.height / 4 * 1, 140, 80), "LMZA压缩文件"))
    //     {
    //         LZMAFile.CompressAsync(Application.dataPath + "/music.mp3", Application.dataPath + "/music.lzma", ShowProgress);
    //     }
    //     if (GUI.Button(new Rect(Screen.width / 4 * 2, Screen.height / 4 * 2, 140, 80), "LMZA解压文件"))
    //     {
    //         LZMAFile.DeCompressAsync(Application.dataPath + "/music.lzma", Application.dataPath + "/lzmamusic.mp3", ShowProgress);
    //     }
    // }
    //
    // public void PackFolderAsync()
    // {
    //     debug.log("UPK打包文件夹");
    //
    //
    //
    //
    //     UPKFolder.PackFolderAsync(PathConfig.GameData + "/pictur", PathConfig.GameData + "/pictur.upk", ShowProgress);
    // }
    // public void UnPackFolderAsync()
    // {
    //     debug.log("UPK解包文件夹");
    //     UPKFolder.UnPackFolderAsync(PathConfig.GameData + "/pictur.upk", PathConfig.GameData + "/new/", ShowProgress);
    // }
    //in D:/xxx
    //out D:/xxx.lzma
    public static void Compress(FileChangeInfo info)
    {
        FileChangeInfo upkinfo = new FileChangeInfo();
        upkinfo.inpath = info.inpath;
        upkinfo.outpath = info.inpath + ".upk";
        upkinfo.progressDelegate = info.progressDelegate;
        upkinfo.finish = () =>
        {
            FileChangeInfo LZMAinfo = new FileChangeInfo();
            LZMAinfo.inpath = upkinfo.outpath;
            LZMAinfo.outpath = info.outpath;
            LZMAinfo.finish = info.finish;
            upkinfo.progressDelegate = info.progressDelegate;
            LZMAFile.CompressAsync(LZMAinfo);
        };
        UPKFolder.PackFolderAsync(upkinfo);
    }

    //in D:/xxx.lzma
    //out D:/xxx
    public static void UnCompress(FileChangeInfo info)
    {
        FileChangeInfo LZMAinfo = new FileChangeInfo();
        LZMAinfo.inpath = info.inpath;
        LZMAinfo.outpath = info.inpath.Remove(info.inpath.Length - ".lzma".Length, ".lzma".Length) + ".udp";
        LZMAinfo.progressDelegate = info.progressDelegate;
        LZMAinfo.finish = () =>
        {
            FileChangeInfo upkinfo = new FileChangeInfo();
            upkinfo.inpath = LZMAinfo.outpath;
            upkinfo.outpath = info.outpath;
            upkinfo.progressDelegate = info.progressDelegate;

            upkinfo.finish = () =>
            {
                info.finish();
                FileIO.Delete(upkinfo.inpath);
            };
            UPKFolder.UnPackFolderAsync(upkinfo);
        };
        LZMAFile.DeCompressAsync(LZMAinfo);
    }

    public static void ShowProgress(long all, long now)
    {
        double progress = (double)now / all;
        Debug.Log("当前进度为: " + progress);
    }
}